package egovframework.npqs.pqi.mobile.jsonparser;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import egovframework.npqs.pqi.mobile.AppStatus;
import egovframework.npqs.pqi.mobile.common.Const;
import egovframework.npqs.pqi.mobile.jsoncontent.ApproveListItem;
import egovframework.npqs.pqi.mobile.jsoncontent.ErrorItem;
import egovframework.npqs.pqi.mobile.util.BrUtilManager;
import egovframework.npqs.pqi.mobile.util.ParameterUtil;

public class ApproveListThread extends BasicThread {

	public interface ApproveListThreadCallback {
		public void OnApproveListCompleted(boolean isNetworkConntected, boolean results, String message);
		public void OnApproveListError(ErrorItem errorItem);
	}

	private String url;
 	private ApproveListThreadCallback callback;
	private HashMap<String, String> searchMap;
	private ArrayList<NameValuePair> post;
	
	/* 파라메터*/
	private boolean pageState;

	
	public ApproveListThread(Context context
			, ApproveListThreadCallback callback
			, HashMap<String, String> searchMap
			, boolean pageState
			, String vpnCnId) {
		super(vpnCnId);
		this.context = context;
		this.callback = callback;
		this.appStatus = (AppStatus) context.getApplicationContext();
		this.searchMap = searchMap;
		this.pageState = pageState;
	}

	@Override
	public void run() {
		Map<String, String> map = new HashMap<String, String>();
		String param = "";
		
		map=searchMap;
		param = parameterReturn(Const.REQ030, (HashMap<String, String>) map);
		
		serviceBroker(param, context);
	}

	@Override
	public void settingObject() {
		// 통신불통상태이면
		if(!appStatus.utilm.isNetworkConnected(context)) {
			callback.OnApproveListCompleted(false, false, "통신에러");
			return;
		}
		
		try {
			json = new JSONObject(returnJson);
			if(json == null) {	// json 값이 null 일때
				callback.OnApproveListError(null);
				return;
			}
			int result = json.getInt(Const.RESULT);
			
			ArrayList<ApproveListItem> arrApproveItem = null;
			if(pageState){
				arrApproveItem = appStatus.getArrApproveItem();
			}else{
				arrApproveItem = new ArrayList<ApproveListItem>();
			}
			
			if(result == 0){	// result 값이 0일 경우
				String data = json.getString(Const.DATA);
				
				ErrorItem errorItem = new ErrorItem();
				errorItem.setrDesc(data);

				appStatus.setArrApproveItem(arrApproveItem);
				callback.OnApproveListError(errorItem);
			}else{	// result 값이 0이 아닐 경우
				JSONArray arrayLogin = json.getJSONArray(Const.DATA);
				ApproveListItem approveItem = null;
				for (int i = 0; i < arrayLogin.length(); i++) {
					JSONObject approve = arrayLogin.getJSONObject(i);
					approveItem = new ApproveListItem();
					approveItem.setFieDe(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(approve, ApproveListItem.FIEDE)));
					approveItem.setOffCd(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(approve, ApproveListItem.OFFCD)));
					approveItem.setRecNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(approve, ApproveListItem.RECNO)));
					approveItem.setValidflagNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(approve, ApproveListItem.VALIDFLAGNM)));
					approveItem.setVenNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(approve, ApproveListItem.VENNM)));
					approveItem.setPrdlstNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(approve, ApproveListItem.PRDLSTNM)));
					approveItem.setDelayAt(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(approve, ApproveListItem.DELAYAT)));
					approveItem.setDelayNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(approve, ApproveListItem.DELAYNM)));
					approveItem.setProgrsNm(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(approve, ApproveListItem.PROGRSNM)));
					approveItem.setTotalRows(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(approve, ApproveListItem.TOTALROWS)));
					approveItem.setResNo(BrUtilManager.isNullCheck(appStatus.utilm.getStringJSONObject(approve, ApproveListItem.RESNO)));
					
					arrApproveItem.add(approveItem);
				}
				
				appStatus.setArrApproveItem(arrApproveItem);
				callback.OnApproveListCompleted(true, true, mResult);
			}
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
	}
}
